package com.smomo.blog.service.front.impl;

import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.smomo.blog.common.RedisConstants;
import com.smomo.blog.common.ResponseResult;
import com.smomo.blog.dto.EmailLoginDTO;
import com.smomo.blog.dto.UserInfoDTO;
import com.smomo.blog.entity.User;
import com.smomo.blog.entity.UserInfo;
import com.smomo.blog.enums.LoginTypeEnum;
import com.smomo.blog.enums.UserStatusEnum;
import com.smomo.blog.exception.BusinessException;
import com.smomo.blog.mapper.FollowedMapper;
import com.smomo.blog.mapper.UserInfoMapper;
import com.smomo.blog.mapper.UserMapper;

import com.smomo.blog.service.front.ApiUserService;
import com.smomo.blog.utils.*;
import com.smomo.blog.vo.UserInfoVO;
import com.smomo.blog.vo.WxUserInfoVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import static com.smomo.blog.common.ResultCode.*;

@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ApiUserServiceImpl implements ApiUserService {

    private final UserMapper userMapper;
    private final UserInfoMapper userInfoMapper;
    /**
     * 邮箱登录
     *
     * @param vo
     * @return
     */
    @Override
    public ResponseResult emailLogin(EmailLoginDTO vo) {

        User user = userMapper.selectNameAndPassword(vo.getEmail(), AesEncryptUtils.aesEncrypt(vo.getPassword()));
        if (user == null) {
            throw new BusinessException(ERROR_PASSWORD.desc);
        }

        if (user.getStatus() == UserStatusEnum.disable.code) {
            throw new BusinessException(DISABLE_ACCOUNT.desc);
        }

        UserInfo userInfo = userInfoMapper.selectById(user.getUserInfoId());

        //七天有效时间  登录
        StpUtil.login(user.getId(), new SaLoginModel().setDevice("PC").setTimeout(60 * 60 * 24 * 7));

        //组装数据
        UserInfoVO userInfoVO = UserInfoVO.builder()
                .id(user.getId())
                .avatar(userInfo.getAvatar())
                .nickname(userInfo.getNickname())
                .intro(userInfo.getIntro())
                .webSite(userInfo.getWebSite())
                .email(user.getUsername())
                .loginType(user.getLoginType())
                .token(StpUtil.getTokenValue())
                .build();

        return ResponseResult.success(userInfoVO);
    }

    @Override
    public ResponseResult selectUserInfo() {
        return null;
    }

    @Override
    public ResponseResult updateUser(UserInfoDTO vo) {
        return null;
    }

    @Override
    public ResponseResult selectUserInfoByToken(String token) {
        return null;
    }


}
